package cn.iocoder.yudao.module.biz.service.emergencyReservePlan;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyReservePlan.vo.BizEmergencyReservePlanCreateReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyReservePlan.vo.BizEmergencyReservePlanExportReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyReservePlan.vo.BizEmergencyReservePlanPageReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyReservePlan.vo.BizEmergencyReservePlanUpdateReqVO;
import cn.iocoder.yudao.module.biz.convert.emergencyReservePlan.BizEmergencyReservePlanConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.emergencyReservePlan.BizEmergencyReservePlanDO;
import cn.iocoder.yudao.module.biz.dal.mysql.emergencyReservePlan.BizEmergencyReservePlanMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.EMERGENCY_RESERVE_PLAN_NOT_EXISTS;

/**
 * 应急预案 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizEmergencyReservePlanServiceImpl implements BizEmergencyReservePlanService {

    @Resource
    private BizEmergencyReservePlanMapper emergencyReservePlanMapper;

    @Override
    public String createEmergencyReservePlan(BizEmergencyReservePlanCreateReqVO createReqVO) {
        // 插入
        BizEmergencyReservePlanDO emergencyReservePlan = BizEmergencyReservePlanConvert.INSTANCE.convert(createReqVO);
        emergencyReservePlanMapper.insert(emergencyReservePlan);
        // 返回
        return emergencyReservePlan.getId();
    }

    @Override
    public void updateEmergencyReservePlan(BizEmergencyReservePlanUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateEmergencyReservePlanExists(updateReqVO.getId());
        // 更新
        BizEmergencyReservePlanDO updateObj = BizEmergencyReservePlanConvert.INSTANCE.convert(updateReqVO);
        emergencyReservePlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteEmergencyReservePlan(String id) {
        // 校验存在
        this.validateEmergencyReservePlanExists(id);
        // 删除
        emergencyReservePlanMapper.deleteById(id);
    }

    private void validateEmergencyReservePlanExists(String id) {
        if (emergencyReservePlanMapper.selectById(id) == null) {
            throw exception(EMERGENCY_RESERVE_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public BizEmergencyReservePlanDO getEmergencyReservePlan(String id) {
        return emergencyReservePlanMapper.selectById(id);
    }

    @Override
    public List<BizEmergencyReservePlanDO> getEmergencyReservePlanList(Collection<String> ids) {
        return emergencyReservePlanMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BizEmergencyReservePlanDO> getEmergencyReservePlanPage(BizEmergencyReservePlanPageReqVO pageReqVO) {
        return emergencyReservePlanMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizEmergencyReservePlanDO> getEmergencyReservePlanList(BizEmergencyReservePlanExportReqVO exportReqVO) {
        return emergencyReservePlanMapper.selectList(exportReqVO);
    }

    @Override
    public List<Map<String, Object>> getEmergencyHistogram() {
        return emergencyReservePlanMapper.getEmergencyHistogram();
    }

}
